/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.samples.countries.ui.peak;

import java.util.Map;

import org.springframework.web.servlet.support.RequestContext;

import pikes.ecma.ExpressionStatement;
import pikes.ecma.FunctionCall;
import pikes.ecma.MemberPropertyAccess;
import pikes.ecma.SourceElement;
import pikes.html.xhtml.Inline;
import pikes.html.xhtml.PCData;
import pikes.peak.Composite;
import pikes.peak.InlineComponent;
import pikes.peak.InternalURI;
import pikes.peak.Label;
import pikes.peak.LabelStyle;
import pikes.peak.Picture;
import pikes.peak.RequestProperty;
import pikes.peak.Paragraph;
import pikes.peak.TextGroupStyle;
import pikes.samples.countries.Country;

public class DetailPage extends BehaviourPage {

	@Override
	protected String getTitle() {
		return "countries.detail.htitle";
	}

	@Override
	protected void buildNavigation(Composite navigationPanel) {
		// detail page does not have content in navigation panel
	}

	@Override
	protected void buildContent(Composite content) {

		Composite backButtonHolder = new Composite();

		backButtonHolder.setClazz("back");
		Picture backPicture = new Picture(new InternalURI("img-back"), "gen.back");
		/*
		 * backPicture.setURI(new EventHandlerURI(new EventHandler() { @Override protected void onEvent() {
		 * history.go(-1); } }));
		 */
		backPicture.setURI(new EventHandlerURI(new RequestProperty<SourceElement>() {

			public SourceElement getValue(Map model, RequestContext requestContext) throws Exception {
				FunctionCall call = new FunctionCall(new MemberPropertyAccess("history", "go"), "-1");
				return new ExpressionStatement(call);
			}

		}));

		backButtonHolder.add(backPicture);

		content.add(new Paragraph("countries.detail.title", TextGroupStyle.LEVEL_2));
		content.add(new Paragraph(Label.SPACE));

		content.add(new Paragraph(new CountryName(), TextGroupStyle.LEVEL_3));

		Label codeLabel = new Label("code", LabelStyle.STRONG);
		codeLabel.addText(":");
		content.add(codeLabel);
		content.add(new CountryCode());

		content.add(Label.LINE_BREAK);

		content.add(backButtonHolder);
	}

}

class CountryName implements InlineComponent {

	public Inline createHtml(Map model, RequestContext requestContext) throws Exception {
		Country country = (Country) model.get("country");
		return new PCData(country.getName());
	}

}

class CountryCode implements InlineComponent {

	public Inline createHtml(Map model, RequestContext requestContext) throws Exception {
		Country country = (Country) model.get("country");
		return new PCData(country.getCode());
	}

}
